package com.gr.service.impl;

import com.gr.dao.TypeMapper;
import com.gr.entity.Type;
import com.gr.service.TypeService;
import com.gr.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class TypeServiceImpl implements TypeService {
    @Override
    public List<Type> selectAll() {
        List<Type> types=null;
        SqlSession session = null;
        try {
            session = MybatisUtils.getSession();
            TypeMapper tm = session.getMapper(TypeMapper.class);
            types=tm.selectAll();

        } finally {
            MybatisUtils.closeSession(session);
        }
        return types;
    }

    @Override
    public Type selectOne(String name) {
        Type type=null;
        SqlSession session = null;
        try {
            session = MybatisUtils.getSession();
            TypeMapper tm = session.getMapper(TypeMapper.class);
            type=tm.selectOne(name);

        } finally {
            MybatisUtils.closeSession(session);
        }
        return type;
    }

    @Override
    public List<Type> selectAllTypeAndTeaNum() {
        List<Type> types=null;
        SqlSession session = null;
        try {
            session = MybatisUtils.getSession();
            TypeMapper tm = session.getMapper(TypeMapper.class);
            types=tm.selectAllTypeAndTeaNum();

        } finally {
            MybatisUtils.closeSession(session);
        }
        return types;
    }

    @Override
    public void deleteById(Integer id) {
        SqlSession session = null;
        try {
            session = MybatisUtils.getSession();
            TypeMapper tm = session.getMapper(TypeMapper.class);
            tm.deleteById(id);
            session.commit();

        } finally {
            MybatisUtils.closeSession(session);
        }
    }
}
